<template>
  <div class="article-comments">
      <!-- 评论列表 -->
      <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
          >
          <van-cell v-for="item in list" :key="item.com_id">
    <van-image
        slot="icon"
        round
        width="30"
        height="30"
        style="margin-right: 10px;"
       :src="item.aut_photo"
        />
   <span style="color: #466b9d;" slot="title">{{ item.aut_name }}</span>
    <div slot="label">
       <p style="color: #363636;">{{ item.content }}</p>
        <p>
           <span style="margin-right: 10px;">{{  item.pubdate | fromNow  }}</span>
            <van-button
            @click="replyFn(item)"
            size="mini" type="default">
                   回复 {{ item.reply_count }}
            </van-button>
        </p>
    </div>
    <van-icon
         @click="likeFn(item)"
          slot="right-icon"
         :name="item.is_liking ? 'like' : 'like-o'"

    />
</van-cell>
      </van-list>
      <!-- /评论列表 -->

      <!-- 发布评论 -->
      <van-cell-group class="publish-wrap">
          <van-field
              clearable
              v-model.trim="content"
              placeholder="请输入评论内容"
              >
              <van-button
              @click="publishFn"
              slot="button" size="mini" type="info">发布</van-button>
          </van-field>
      </van-cell-group>
      <!-- /发布评论 -->
      <van-action-sheet v-model="show" title="评论回复">
      <CommReply
      v-if="show"
      @addCommonCount="addCommonCount"
      :artId="artId"
      :comment="currentComment"></CommReply>
</van-action-sheet>
  </div>
</template>

<script>
import {
  getArtilceCommon, addArticleCommon, likeCommon, unLikeCommon
} from '@/api/articleDetail'
import CommReply from './commReply'
export default {
  name: 'ArticleCommon',
  components: {
    CommReply
  },
  props: {

  },
  computed: {
    artId() {
      return this.$route.params.id
    }
  },
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      content: '',
      show: false,
      ToCommon: {},
      currentComment: ''
    }
  },

  methods: {
    addCommonCount() {
      console.log(123)
      this.currentComment.reply_count++
    },
    replyFn(item) {
      // 显示 action-sheet
      // this.isShowReply = true
      this.currentComment = item
      this.show = true
    },
    async onLoad () {
      console.log(this.artId)
      const res = await getArtilceCommon(this.artId, this.offset)
      this.list = [...this.list, ...res.data.results]
      this.loading = false
      this.offset = res.data.last_id
      if (res.data.results.length === 0) {
        this.finished = true
      }
    },
    async publishFn() {
      if (this.content.length === '') { return this.$toast('请输入内容') }
      const res = await addArticleCommon(this.artId, this.content)
      this.list.unshift(res.data.new_obj)
      this.$toast('发布成功')
      this.content = ''
    },
    async likeFn(item) {
    // console.log(item)
      if (item.is_liking) {
      // 已经点赞了； ==>  取消点赞
        const res = await unLikeCommon(item.com_id)
        console.log(res)
      } else {
      // 点赞
        const res = await likeCommon(item.com_id)
        console.log(res)
      }
      this.$toast('操作成功')
      // 还要修改item.is_liking 的值，取反操作
      item.is_liking = !item.is_liking
    }
  }
}
</script>

<style scoped lang='less'>
  // 发表评论的区域是固定在下端的
  .publish-wrap {
      position: fixed;
      left: 0;
      bottom: 0;
      width: 100%;
  }
  // 给发表评论区空出地方
  .van-list {
      margin-bottom: 45px;
  }
</style>
